import { httpRequest } from '@/utils/http';
import { OssFile } from './model';
import {IDS, PageQuery} from "@/api/base";
import {base64StringToFile} from "@/utils/file";
import {getUid} from "@/tmui/tool/function/util";

enum Api {
  root = '/resource/oss',
  ossList = '/resource/oss/list',
  ossInfo = '/resource/oss/listByIds',
  ossUpload = '/resource/oss/upload',
  ossDownload = '/resource/oss/download',
}

export function ossList(params: any) {
  return httpRequest.get<OssFile[]>({url: Api.ossList, data: params});
}

export function ossInfo(ossIds: IDS) {
  return httpRequest.get<OssFile>({ url: Api.ossInfo + '/' + ossIds });
}

/**
 * @param fileContent
 * @param successMsg
 * @returns
 */
export function ossUpload(fileContent: string) {
  if (fileContent && fileContent.startsWith('data:image')) {
    return httpRequest.uploadFile<{ imgUrl: string }>({
      url: Api.ossUpload,
      file: base64StringToFile(fileContent, getUid() + '.png'),
      name: 'file',
    });
  }
  return httpRequest.uploadFile<{ imgUrl: string }>({
    url: Api.ossUpload,
    filePath: fileContent,
    name: 'file',
  });
}
